var gulp = require('gulp');
var util = require('gulp-util');
var sass = require('gulp-sass');

var path = require('path');
var argv = require('yargs')
    .argv;
var sync = require('gulp-watch');

/* Webpack's configuration */
var webpack = require('webpack');
var build = require('webpack-stream');
var named = require('vinyl-named');

var watch = argv.watch === 'false' ? false : true;
var debug = argv.debug === 'false' ? false : true;

var config = {
    addVendor: function(name, path) {
        if (name && path) {
            this.resolve.alias[name] = path;
            this.module.noParse.push(new RegExp(path));
        }
    },
    resolve: {
        root: [],
        modulesDirectories: ['node_modules'],
        alias: {},
        extensions: ['', '.js']
    },
    resolveLoader: {
        root: []
    },
    plugins: [],
    watch: watch,
    module: {
        noParse: [],
        loaders: [{
            test: /\.js$|\.jsx$/,
            loader: 'babel-loader'
        }]
    }
};

(function(gulp, sync, util) {
    if (process.env.NODE_PATH) {
        var NODE_PATH = process.env.NODE_PATH.split(path.delimiter);
        config.resolve.root = NODE_PATH;
        config.resolveLoader.root = NODE_PATH;
    }

    if (debug) {
        config.devtool = '#inline-source-map';
        config.debug = true;
    } else {
        config.plugins = config.plugins.concat(new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify('production')
            }
        }), new webpack.optimize.DedupePlugin(), new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            },
            output: {
                comments: false
            }
        }));
    }

    var code = ['./src/jsx/**/*.jsx'];
    var dest = './war/tpl';

    gulp.task('webpack', function() {
        gulp.src(code)
            .pipe(named(function(file) {
                return file.relative.slice(0, -4);
            }))
            .pipe(build(config))
            .pipe(gulp.dest(dest));
    });
})(gulp, sync, util);
/* Webpack's configuration */

/* Sync Changed File(s) */
(function(gulp, sync, util) {
    var code = ['./src/scss/**/*.scss'];
    var dest = './war/tpl';

    gulp.task('dUTwJoaV', function() {
        gulp.src(code)
            .pipe(sass({
                    outputStyle: debug ? 'expanded' : 'compressed'
                })
                .on('error', sass.logError))
            .pipe(gulp.dest(dest));
    });
    gulp.task('scss:watch', function() {
        gulp.watch(code, ['dUTwJoaV']);
    });
})(gulp, sync, util);

(function(gulp, sync, util) {
    var code = ['./war/**/*', '!./war/WEB-INF/classes/META-INF/**/*'];
    var dest = './target/ROOT.jar';

    gulp.task('sync:watch', function() {
        sync(code, function(vinyl) {
                util.log(util.colors.green('SYNC: ' + vinyl.relative));
            })
            .pipe(gulp.dest(dest));
    });
})(gulp, sync, util);
/* Sync Changed File(s) */

gulp.task('default', ['webpack', 'scss:watch', 'sync:watch']);
